﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading;
using HslCommunication.LogNet;

namespace HslCommunication.Profinet.Toledo
{
	/// <summary>
	/// 托利多电子秤的串口服务器对象
	/// </summary>
	// Token: 0x02000045 RID: 69
	public class ToledoSerial
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x060005BF RID: 1471 RVA: 0x0001AABE File Offset: 0x00018CBE
		public ToledoSerial()
		{
			this.serialPort = new SerialPort();
			this.serialPort.RtsEnable = true;
			this.serialPort.DataReceived += this.SerialPort_DataReceived;
		}

		// Token: 0x060005C0 RID: 1472 RVA: 0x0001AAFC File Offset: 0x00018CFC
		private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			List<byte> list = new List<byte>();
			byte[] array = new byte[1024];
			for (;;)
			{
				Thread.Sleep(20);
				bool flag = this.serialPort.BytesToRead < 1;
				if (flag)
				{
					break;
				}
				try
				{
					int num = this.serialPort.Read(array, 0, Math.Min(this.serialPort.BytesToRead, array.Length));
					byte[] array2 = new byte[num];
					Array.Copy(array, 0, array2, 0, num);
					list.AddRange(array2);
				}
				catch (Exception ex)
				{
					ILogNet logNet = this.logNet;
					if (logNet != null)
					{
						logNet.WriteException(this.ToString(), "SerialPort_DataReceived", ex);
					}
					return;
				}
			}
			bool flag2 = list.Count == 0;
			if (!flag2)
			{
				ToledoSerial.ToledoStandardDataReceivedDelegate onToledoStandardDataReceived = this.OnToledoStandardDataReceived;
				if (onToledoStandardDataReceived != null)
				{
					onToledoStandardDataReceived(this, new ToledoStandardData(list.ToArray()));
				}
			}
		}

		/// <summary>
		/// 初始化串口信息，9600波特率，8位数据位，1位停止位，无奇偶校验<br />
		/// Initial serial port information, 9600 baud rate, 8 data bits, 1 stop bit, no parity
		/// </summary>
		/// <param name="portName">端口号信息，例如"COM3"</param>
		// Token: 0x060005C1 RID: 1473 RVA: 0x0001ABEC File Offset: 0x00018DEC
		public void SerialPortInni(string portName)
		{
			this.SerialPortInni(portName, 9600);
		}

		/// <summary>
		/// 初始化串口信息，波特率，8位数据位，1位停止位，无奇偶校验<br />
		/// Initializes serial port information, baud rate, 8-bit data bit, 1-bit stop bit, no parity
		/// </summary>
		/// <param name="portName">端口号信息，例如"COM3"</param>
		/// <param name="baudRate">波特率</param>
		// Token: 0x060005C2 RID: 1474 RVA: 0x0001ABFB File Offset: 0x00018DFB
		public void SerialPortInni(string portName, int baudRate)
		{
			this.SerialPortInni(portName, baudRate, 8, StopBits.One, Parity.None);
		}

		/// <summary>
		/// 初始化串口信息，波特率，数据位，停止位，奇偶校验需要全部自己来指定<br />
		/// Start serial port information, baud rate, data bit, stop bit, parity all need to be specified
		/// </summary>
		/// <param name="portName">端口号信息，例如"COM3"</param>
		/// <param name="baudRate">波特率</param>
		/// <param name="dataBits">数据位</param>
		/// <param name="stopBits">停止位</param>
		/// <param name="parity">奇偶校验</param>
		// Token: 0x060005C3 RID: 1475 RVA: 0x0001AC0C File Offset: 0x00018E0C
		public void SerialPortInni(string portName, int baudRate, int dataBits, StopBits stopBits, Parity parity)
		{
			bool isOpen = this.serialPort.IsOpen;
			if (!isOpen)
			{
				this.serialPort.PortName = portName;
				this.serialPort.BaudRate = baudRate;
				this.serialPort.DataBits = dataBits;
				this.serialPort.StopBits = stopBits;
				this.serialPort.Parity = parity;
				this.PortName = this.serialPort.PortName;
				this.BaudRate = this.serialPort.BaudRate;
			}
		}

		/// <summary>
		/// 根据自定义初始化方法进行初始化串口信息<br />
		/// Initialize the serial port information according to the custom initialization method
		/// </summary>
		/// <param name="initi">初始化的委托方法</param>
		// Token: 0x060005C4 RID: 1476 RVA: 0x0001AC94 File Offset: 0x00018E94
		public void SerialPortInni(Action<SerialPort> initi)
		{
			bool isOpen = this.serialPort.IsOpen;
			if (!isOpen)
			{
				this.serialPort.PortName = "COM5";
				this.serialPort.BaudRate = 9600;
				this.serialPort.DataBits = 8;
				this.serialPort.StopBits = StopBits.One;
				this.serialPort.Parity = Parity.None;
				initi(this.serialPort);
				this.PortName = this.serialPort.PortName;
				this.BaudRate = this.serialPort.BaudRate;
			}
		}

		/// <summary>
		/// 打开一个新的串行端口连接<br />
		/// Open a new serial port connection
		/// </summary>
		// Token: 0x060005C5 RID: 1477 RVA: 0x0001AD30 File Offset: 0x00018F30
		public void Open()
		{
			bool flag = !this.serialPort.IsOpen;
			if (flag)
			{
				this.serialPort.Open();
			}
		}

		/// <summary>
		/// 获取一个值，指示串口是否处于打开状态<br />
		/// Gets a value indicating whether the serial port is open
		/// </summary>
		/// <returns>是或否</returns>
		// Token: 0x060005C6 RID: 1478 RVA: 0x0001AD5E File Offset: 0x00018F5E
		public bool IsOpen()
		{
			return this.serialPort.IsOpen;
		}

		/// <summary>
		/// 关闭当前的串口连接<br />
		/// Close the current serial connection
		/// </summary>
		// Token: 0x060005C7 RID: 1479 RVA: 0x0001AD6C File Offset: 0x00018F6C
		public void Close()
		{
			bool isOpen = this.serialPort.IsOpen;
			if (isOpen)
			{
				this.serialPort.Close();
			}
		}

		/// <inheritdoc cref="P:HslCommunication.Core.Net.NetworkBase.LogNet" />
		// Token: 0x17000133 RID: 307
		// (get) Token: 0x060005C8 RID: 1480 RVA: 0x0001AD98 File Offset: 0x00018F98
		// (set) Token: 0x060005C9 RID: 1481 RVA: 0x0001ADB0 File Offset: 0x00018FB0
		public ILogNet LogNet
		{
			get
			{
				return this.logNet;
			}
			set
			{
				this.logNet = value;
			}
		}

		/// <summary>
		/// 获取或设置一个值，该值指示在串行通信中是否启用请求发送 (RTS) 信号。<br />
		/// Gets or sets a value indicating whether the request sending (RTS) signal is enabled in serial communication.
		/// </summary>
		// Token: 0x17000134 RID: 308
		// (get) Token: 0x060005CA RID: 1482 RVA: 0x0001ADBA File Offset: 0x00018FBA
		// (set) Token: 0x060005CB RID: 1483 RVA: 0x0001ADC7 File Offset: 0x00018FC7
		public bool RtsEnable
		{
			get
			{
				return this.serialPort.RtsEnable;
			}
			set
			{
				this.serialPort.RtsEnable = value;
			}
		}

		/// <summary>
		/// 当前连接串口信息的端口号名称<br />
		/// The port name of the current connection serial port information
		/// </summary>
		// Token: 0x17000135 RID: 309
		// (get) Token: 0x060005CC RID: 1484 RVA: 0x0001ADD6 File Offset: 0x00018FD6
		// (set) Token: 0x060005CD RID: 1485 RVA: 0x0001ADDE File Offset: 0x00018FDE
		public string PortName { get; private set; }

		/// <summary>
		/// 当前连接串口信息的波特率<br />
		/// Baud rate of current connection serial port information
		/// </summary>
		// Token: 0x17000136 RID: 310
		// (get) Token: 0x060005CE RID: 1486 RVA: 0x0001ADE7 File Offset: 0x00018FE7
		// (set) Token: 0x060005CF RID: 1487 RVA: 0x0001ADEF File Offset: 0x00018FEF
		public int BaudRate { get; private set; }

		/// <summary>
		/// 当接收到一条新的托利多的数据的时候触发
		/// </summary>
		// Token: 0x14000008 RID: 8
		// (add) Token: 0x060005D0 RID: 1488 RVA: 0x0001ADF8 File Offset: 0x00018FF8
		// (remove) Token: 0x060005D1 RID: 1489 RVA: 0x0001AE30 File Offset: 0x00019030
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event ToledoSerial.ToledoStandardDataReceivedDelegate OnToledoStandardDataReceived;

		/// <inheritdoc />
		// Token: 0x060005D2 RID: 1490 RVA: 0x0001AE68 File Offset: 0x00019068
		public override string ToString()
		{
			return base.ToString();
		}

		// Token: 0x04000185 RID: 389
		private SerialPort serialPort;

		// Token: 0x04000186 RID: 390
		private ILogNet logNet;

		/// <summary>
		/// 托利多数据接收时的委托
		/// </summary>
		/// <param name="sender">数据发送对象</param>
		/// <param name="toledoStandardData">数据对象</param>
		// Token: 0x02000288 RID: 648
		// (Invoke) Token: 0x06002411 RID: 9233
		public delegate void ToledoStandardDataReceivedDelegate(object sender, ToledoStandardData toledoStandardData);
	}
}
